package com.sk.cardeal.act.recommend;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sk.cardeal.R;
import com.sk.cardeal.adapter.ConsultAdapter;
import com.sk.cardeal.base.BaseFragment;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.entity.ConsultListBean;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.MyUtils;
import com.sk.cardeal.view.MultiStateView;
import com.sk.cardeal.view.SimpleMultiStateView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by sk on 2018/2/1.
 */

public class RecommendConsultFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, HandleDataCallBack {

    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    @BindView(R.id.mPtrFrameLayout)
    PtrClassicFrameLayout mPtrFrameLayout;
    Unbinder unbinder;
    @BindView(R.id.SimpleMultiStateView)
    SimpleMultiStateView simpleMultiStateView;
    Unbinder unbinder1;

    private boolean isRefresh = false;
    private boolean isLoadMore = false;
    private ConsultAdapter adapter;
    private int type;
    private int page = 0;
    private List<ConsultListBean.ResultListBean> allDataList;

    @Override
    protected int getContentView() {
        return R.layout.layout_recycler;
    }

    public static RecommendConsultFragment getInstance(int type) {
        RecommendConsultFragment fragment = new RecommendConsultFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initStateView();

        allDataList = new ArrayList<>();

        mPtrFrameLayout.disableWhenHorizontalMove(true);

        mPtrFrameLayout.setPtrHandler(new PtrHandler() {

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, dataRyc, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                isRefresh = true;
                doRefresh();
            }
        });

        type = getArguments().getInt("type");
        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new ConsultAdapter(R.layout.item_conuslt, allDataList);
        dataRyc.setAdapter(adapter);
        adapter.setOnItemClickListener(this);

        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.empty_view, null);
        TextView textView = emptyView.findViewById(R.id.content);
        textView.setText("暂无改类资讯！");
        adapter.setEmptyView(emptyView);
        adapter.setOnItemClickListener(this);
        adapter.setOnLoadMoreListener(this, dataRyc);
        AppApi.getConsultList(MyUtils.getUser(mContext).getId(), type, "", page, this, 0, getActivity());

    }

    private void doRefresh() {

        page = 0;
        AppApi.getConsultList(MyUtils.getUser(mContext).getId(), type, "", page, this, 0, getActivity());

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent  = new Intent(mContext,ArticleDetailsActivity.class);
        intent.putExtra("title",allDataList.get(position).getTitle());
        intent.putExtra("data",allDataList.get(position).getInfo());
        startActivity(intent);
    }

    @Override
    public void onLoadMoreRequested() {
        isLoadMore = true;
        page++;
        AppApi.getConsultList(MyUtils.getUser(mContext).getId(), type, "", page, this, 0, getActivity());
    }

    @Override
    public void handlerSuccessData(String result, int tag) {

        showSuccess();

        ConsultListBean consultListBean = JSON.parseObject(result, ConsultListBean.class);
        List<ConsultListBean.ResultListBean> dataBeanList = consultListBean.getResultList();

        if (isRefresh) {
            isRefresh = false;
            mPtrFrameLayout.refreshComplete();
        }

        if (page == 0) {
            allDataList.clear();
        }

        allDataList.addAll(dataBeanList);
        if (allDataList.size() < 10) {
            adapter.setEnableLoadMore(false);
        } else {
            adapter.setEnableLoadMore(true);
        }
        if (isLoadMore) {
            adapter.loadMoreComplete();
            if (dataBeanList.size() == 0) {
                adapter.loadMoreEnd();
            }
            isLoadMore = false;
        }
        adapter.notifyDataSetChanged();

    }

    @Override
    public void handlerFailData(String failString, int tag) {

    }

    @Override
    public void handleErrorData(String error, int tag) {

    }

    private void initStateView() {
        if (simpleMultiStateView == null) return;
        simpleMultiStateView.setEmptyResource(R.layout.view_empty)
                .setRetryResource(R.layout.view_retry)
                .setLoadingResource(R.layout.view_loading)
                .setNoNetResource(R.layout.view_nonet)
                .build()
                .setonReLoadlistener(new MultiStateView.onReLoadlistener() {
                    @Override
                    public void onReload() {
                    }
                });
    }

    public void showLoading() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showLoadingView();
        }
    }

    public void showSuccess() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showContent();
        }
    }

    public void showFaild() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showErrorView();
        }
    }

    public void showNoNet() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showNoNetView();
        }
    }
}
